package com.buydeem.share;

/**
 * 释放对象锁的时机
 *
 * @author zengchao
 * @date 2021-07-21 12:04:04
 * @since 1.0
 */
public class WaitNotifyDemo4 {
    static final Object LOCK = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (LOCK){
                    System.out.println("线程t1进入同步代码块获取到锁");
                    try {
                        LOCK.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程t1 wait后继续执行");
                }
            }
        });
        t1.start();

        Thread.sleep(1000);

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (LOCK){
                    System.out.println("线程t2进入同步代码块");
                    System.out.println("线程t1的状态:"+t1.getState());
                    LOCK.notify();
                    System.out.println("线程t2已执行notify");
                    System.out.println("线程t1的状态:"+t1.getState());
                }
            }
        });
        t2.start();

        t1.join();
        t2.join();
    }
}
